package com.xsp.mybatisxmlsqlparser.replacer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpecialMarkReplacer {

    public static final String DOLLARSTRINGP = "lll_$1_lll";
    public static final String DOLLARSTRINGA = "lll_(.*?)_lll";

    public static final String WELLMARKSTRINGP = "hhh_$1_hhh";
    public static final String WELLMARKSTRINGA = "hhh_(.*?)_hhh";

    public static final String DOLLARREGEXP = "\\$\\{$1\\}";
    public static final String DOLLARREGEXA = "\\$\\{(.*?)\\}";

    public static final String WELLMARKREGEXP = "\\#\\{$1\\}";
    public static final String WELLMARKREGEXA = "\\#\\{(.*?)\\}";

    public static final String COMMA_PLACEHOLDER = "@cccc@cccc@";
    public static final String EQUALS_PLACEHOLDER = "@@eee@@eee@@";

    public static final String CUSTOM_PLACEHOLDER = "_(.*?)_";

    /**
     * 检查字符串是否包含与正则表达式匹配的子串
     *
     * @param input 要检查的字符串
     * @return 如果匹配返回true，否则返回false
     */
    public static boolean containsWellMarkString(String input, String... regex) {
        for (String reg : regex) {
            Pattern pattern = Pattern.compile(reg);
            Matcher matcher = pattern.matcher(input);
            if (matcher.find()) {
                return true;
            }
        }
        return false;
    }


    /**
     * 根据表达式替换规则替换字符串
     *
     * @param input       输入字符串
     * @param regex       正则表达式
     * @param replacement 替换规则
     * @return 转换结果
     * @use 示例：
     * 将 lll_任意字符_lll 替换为 ${任意字符}
     * String toDollar = replacePattern(original, "lll_(.*?)_lll", "\\$\\{$1\\}");
     * System.out.println("Replaced to ${}: " + toDollar);
     * 将 ${任意字符} 替换回 lll_任意字符_lll
     * String toLll = replacePattern(toDollar, "\\$\\{(.*?)\\}", "lll_$1_lll");
     * System.out.println("Replaced back to lll_: " + toLll);
     */
    public static String replacePattern(String input, String regex, String replacement) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        StringBuilder result = new StringBuilder();

        while (matcher.find()) {
            String matchedGroup = matcher.group(1);
            if (regex.equals(DOLLARREGEXA) || regex.equals(WELLMARKREGEXA)) {
                matchedGroup = replaceOrRestoreSpecialCharacters(matchedGroup, true);
            }
            matcher.appendReplacement(result, replacement.replace("$1", matchedGroup));
        }
        matcher.appendTail(result);
        return result.toString();
    }


    /**
     * 根据需要替换逗号和等号，或者恢复它们
     *
     * @param input   输入字符串
     * @param replace true 表示替换特殊字符，false 表示恢复特殊字符
     * @return 替换或恢复后的字符串
     */
    public static String replaceOrRestoreSpecialCharacters(String input, boolean replace) {
        if (replace) {
            // 如果需要替换，并且字符串中包含逗号或等号
            if (input.contains(",") || input.contains("=")) {
                // 替换逗号和等号
                input = input.replace(",", COMMA_PLACEHOLDER).replace("=", EQUALS_PLACEHOLDER);
            }
        } else {
            // 如果需要恢复，并且字符串中包含替换后的字符串
            if (input.contains(COMMA_PLACEHOLDER) || input.contains(EQUALS_PLACEHOLDER)) {
                // 恢复逗号和等号
                input = input.replace(COMMA_PLACEHOLDER, ",").replace(EQUALS_PLACEHOLDER, "=");
            }
        }
        return input;
    }

}
